/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findKthLargest = function (nums, k) {
    let map = new Map()
    let len = nums.length
    for (let i = 0; i < len; i++) {
        map.set(nums[i], 1 + (map.get(nums[i]) || 0))
    }
    console.log('map:', map)
    let bucket = []
    map.forEach((value, key) => {
        // bucket[value] = bucket[value] ? bucket[value].push(key) : [key]
        if (!bucket[value]) {
            bucket[value] = [key]
        } else {
            bucket[value].push(key)
        }
    })
    console.log('bucket:', bucket)
    let i = bucket.length - 1
    console.log('i:',i)
    while (i >= 0 && k > 0) {
        if (bucket[i]) {
            k -= i * bucket[i].length
        }
        i--
    }
    console.log('i,k:',i,k)
    if (k <= 0) {
        return bucket[i+1].sort((a,b)=>b-a).at(k/(i+1)-1)
    }
};

console.log(findKthLargest([3,2,3,1,2,4,5,5,6], 4))